package com.icarbonx.dap.notification.util

import java.text.SimpleDateFormat
import java.util.Calendar

/**
 * Created by Lenovo on 2018/8/13.
 */
object DateUtil {
  /**
   *
   * @param format  yyyyMMdd  or yyyyMMddHH
   * @return
   */
  def getTodayDateTime(format: String): Long = {
    val cal = Calendar.getInstance()
    val dateFormat: SimpleDateFormat = new SimpleDateFormat(format)
    val today = dateFormat.format(cal.getTime)
    dateFormat.parse(today).getTime
  }

  def getTodayDateTime(): Long = {
    val cal = Calendar.getInstance()
    cal.getTimeInMillis
  }

  def getTimeStampFormat(timestamp: Long, format: String = "yyyy-MM-dd"): String = {
    val cal = Calendar.getInstance()
    val dateFormat: SimpleDateFormat = new SimpleDateFormat(format)
    cal.setTimeInMillis(timestamp)
    dateFormat.format(cal.getTime)
  }


  def getYesterdayFormat(format: String = "yyyy-MM-dd"): String = {
    val cal = Calendar.getInstance()
    val dateFormat: SimpleDateFormat = new SimpleDateFormat(format)
    cal.setTimeInMillis(cal.getTimeInMillis - 24 * 60 * 60 * 1000)
    dateFormat.format(cal.getTime)
  }

  def getTodayFormat(format: String = "yyyy-MM-dd"): String = {
    val cal = Calendar.getInstance()
    val dateFormat: SimpleDateFormat = new SimpleDateFormat(format)
    dateFormat.format(cal.getTime)
  }

  def main(args: Array[String]) {
    println(getTodayDateTime("yyyyMMdd"))
  }

  def getYesterdayTimestamp(): Long = {
    getDateTimestamp(getYesterdayFormat())
  }

  def getDateTimestamp(dateStr: String, format: String = "yyyy-MM-dd"): Long = {
    val dateFormat: SimpleDateFormat = new SimpleDateFormat(format)
    dateFormat.parse(dateStr).getTime
  }

  def getDateTime(timestamp: Long): List[Int] = {
    val cal = Calendar.getInstance()
    cal.setTimeInMillis(timestamp)
    List(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH) + 1, cal.get(Calendar.DAY_OF_MONTH),
      cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE), cal.get(Calendar.SECOND))
  }

  def calStartAndDuration(hour: String, duration: String): (Long, Long) = {
    val todayTime = getTodayDateTime("yyyy-MM-dd")
    (todayTime + hour.toInt * 60 * 60 * 1000L, todayTime + hour.toInt * 60 * 60 * 1000 + duration.toInt * 1000L)
  }


  def getTodayDateFoomat(timestamp: Long, format: String): String = {
    val cal = Calendar.getInstance()
    val dateFormat: SimpleDateFormat = new SimpleDateFormat(format)
    cal.setTimeInMillis(timestamp)
    dateFormat.format(cal.getTime)
  }

  def getTodayATime(hour: Int, minute: Int = 0): Long = {
    val todayStamp = getTodayDateTime("yyyyMMdd")
    todayStamp + hour * 60 * 60 * 1000 + minute * 60 * 1000
  }

  def getOneDayFromToday(day: Int): Long = {
    val cal: Calendar = Calendar.getInstance()
    cal.add(Calendar.DATE, -day)
    cal.getTimeInMillis()
  }
}
